package com.zuoban.shiro.chain;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.zuoban.util.JwtUtil;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Date;

/**
 * token过期 需要拦截
 */
@Component
@Order(2)
public class ExpireTokenChain implements AuthChain{

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public BigInteger authentication(String token) {
        DecodedJWT claims = jwtUtil.getClaims(token);
        Date expiresAt = claims.getExpiresAt();
        if (expiresAt.before(new Date())){
            throw new RuntimeException("token已过期，请重新登陆");
        }else {
            return new BigInteger(claims.getClaims().get("userId").asString());
        }
    }
}
